package com.dimong.www.loan.ui.act;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.dimong.loan.sdk.ui.WeakHandler;
import com.dimong.loan.sdk.ui.act.BaseFragmentActivity;
import com.dimong.loan.sdk.utils.SharedUtil;
import com.dimong.www.loan.BuildConfig;
import com.dimong.www.loan.R;
import com.dimong.www.loan.app.MyApplication;
import com.dimong.www.loan.bean.UpgradeBean;
import com.dimong.www.loan.broadcastreceiver.BroadcastReceiverAction;
import com.dimong.www.loan.cache.UpgradeCache;
import com.dimong.www.loan.cache.UserIndexPageCache;
import com.umeng.analytics.MobclickAgent;

public class SplashActivity extends BaseFragmentActivity {
    private WeakHandler weakHandler;
    private ImageView start;

    private ComponentName mDefault;
    private ComponentName mDouble11;
    private PackageManager mPm;
    private UpgradeBean upgradeBean;

    BroadcastReceiver receiver =new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            upgradeBean = UpgradeCache.getInstance().getUpgradeBean();
        }
    };
    @Override
    public void handleActivityMessage(Message msg) {
        super.handleActivityMessage(msg);
        if (upgradeBean != null) {
            if ("1".equals(upgradeBean.getIs_replace())) {
                Intent intent = new Intent(SplashActivity.this, Main2Activity.class);
                SplashActivity.this.startActivity(intent);
            } else {
                if (SharedUtil.getIsFirstOpen()) {
                    Intent intent = new Intent(SplashActivity.this, GuidePageActivity.class);
                    SplashActivity.this.startActivity(intent);
                } else {
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    SplashActivity.this.startActivity(intent);
                }
            }
        } else {
            Intent intent = new Intent(SplashActivity.this, Main2Activity.class);
            SplashActivity.this.startActivity(intent);
        }
        finish();
    }

    /**
     * 初始化一些第三方资源，比如友盟、环信等
     */

    private void initBefore() {
//		MyApplication.user = JimiDBHelper.getInstance().getUserInfoDBHelper().getUserInfo();
//		if (MyApplication.user != null) {
//			HttpUtil.openAppFirst(new OnFunctionListener() {
//				@Override
//				public void onSuccess(Object object) {
//				}
//			});
//		}
//
//		if (BuildConfig.DEBUG) {
//			CrashReport.initCrashReport(this, "900016475", true);
//			MobclickAgent.setCatchUncaughtExceptions(false);
//			MobclickAgent.setDebugMode(true);
//		} else {
//			MobclickAgent.setDebugMode(false);
//			MobclickAgent.setCatchUncaughtExceptions(true);
//		}
//		MobclickAgent.openActivityDurationTrack(false);
//		AVOSCloud.initialize(this, "hp85h9Fu8FKR38dhYUEvJStB-gzGzoHsz", "6jcAAxcggLnrFttrgaqH853S");
//		AVOSCloud.setLastModifyEnabled(true);
//		AVOSCloud.setDebugLogEnabled(false);
    }

    @Override
    public void initIntentParam(Intent intent) {

    }

    @Override
    public void beforeInitView() {

    }

    @Override
    public int getContentView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        return R.layout.act_splash;
    }

    @Override
    public void initView() {
        start = findViewById(R.id.start);
        upgradeBean = UpgradeCache.getInstance().getUpgradeBean();
        if (upgradeBean == null) {
            String result = SharedUtil.getPreferStr("upgradeBean");
            if (!TextUtils.isEmpty(result)) {
                upgradeBean = gson.fromJson(result, UpgradeBean.class);
            }
        }
        if (upgradeBean != null) {
            if ("1".equals(upgradeBean.getIs_replace())) {
                start.setImageResource(R.drawable.start2);
            } else {
                start.setImageResource(R.drawable.start);
            }
        } else {
            start.setImageResource(R.drawable.start2);
        }
    }

    public void changeIcon11(View view) {
        disableComponent(mDefault);
        enableComponent(mDouble11);
    }

    private void enableComponent(ComponentName componentName) {
        mPm.setComponentEnabledSetting(componentName,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
    }

    private void disableComponent(ComponentName componentName) {
        mPm.setComponentEnabledSetting(componentName,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
    }

    @Override
    public void setViewStatus() {
//        mDefault = getComponentName();
//        mDouble11 = new ComponentName(
//            getBaseContext(),
//            "com.dimong.www.loan.ui.act.TestActivity");
        mPm = getApplicationContext().getPackageManager();
        PackageManager pm = getPackageManager();
//        pm.setComponentEnabledSetting(getComponentName(),
//            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
//        pm.setComponentEnabledSetting(new ComponentName(SplashActivity.this, TestActivity.class.getName()),
//            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        try {
//            PackageManager pm = getPackageManager();
            PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);
            int versioncode = pi.versionCode;
            int oldCode = SharedUtil.getPreferInt("app_version", 0);
            if (versioncode > oldCode) {
                SharedUtil.saveIsFirstOpen(true);
                SharedUtil.setPreferInt("app_version", versioncode);
            }
        } catch (Exception e) {

        }

        weakHandler = new WeakHandler(this);
        weakHandler.sendEmptyMessageDelayed(0, 2000l);

        UserIndexPageCache.getInstance().loadCache();
        UpgradeCache.getInstance().loadCache();

        if (BuildConfig.DEBUG) {
            MobclickAgent.setCatchUncaughtExceptions(false);
        } else {
            MobclickAgent.setCatchUncaughtExceptions(true);
        }

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastReceiverAction.REFRESH_UPGRADE);
        LocalBroadcastManager.getInstance(MyApplication.getAppContext()).registerReceiver(receiver,intentFilter);
    }

    @Override
    public void onActResume() {

    }

    @Override
    public void onActPause() {

    }

    @Override
    public void onActDestroy() {
        LocalBroadcastManager.getInstance(MyApplication.getAppContext()).unregisterReceiver(receiver);
    }

    @Override
    public void onActRestart() {

    }

    @Override
    public void onActStop() {

    }

    @Override
    public void onActStart() {

    }

    @Override
    public void onBackPressed() {
    }
}
